﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPMS.Log.Core;
using System.IO;

namespace SPMS.Log.Appender
{
    /// <summary>
    /// 控制台日志记录器
    /// </summary>
    /// <remarks>用于将日志信息输出到控制台</remarks>
    /// <history>
    /// [zanezeng]               2009/10/9 13:29    创建
    /// </history>
    public sealed class ConsoleLogAppender : LiteralLogAppender
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="ConsoleLogAppender"/>的新实例
        /// </summary>
        /// <param name="name">当前日志记录器的名称</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/10 10:13    创建
        /// </history>
        public ConsoleLogAppender(string name)
            : base(name)
        {
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得或者设置是否将日志信息输出到错误流
        /// </summary>
        /// <value>
        /// 	<c>true</c>：将日志信息输出到错误流<br/>
        /// 	<c>false</c>：不将日志信息输出到错误流
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/12 17:25    创建
        /// </history>
        public bool IsWriteToErrorStream
        {
            get
            {
                return this.m_IsWriteToErrorStream;
            }
            set
            {
                this.m_IsWriteToErrorStream = value;
            }
        }

        #endregion

        #region ---Protected Method

        /// <summary>
        /// 通知输出给定的日志条目
        /// </summary>
        /// <param name="entry">要输出的日志条目</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 15:26    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/9 13:30    创建
        /// </history>
        protected override void OnAppend(LogEntry entry)
        {
            //获得日志级别对应的颜色
            ConsoleColor color = this.GetLogLevelReanderColor(entry.Level);

            //设置显示颜色
            Console.ForegroundColor = color;

            //调用基类实现
            base.OnAppend(entry);

            //重置显示颜色
            Console.ResetColor();
        }

        /// <summary>
        /// 准备用于接收日志信息的写入器
        /// </summary>
        /// <returns>用于接收日志信息的写入器</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/28 10:56    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/12 16:28    创建
        /// </history>
        protected override SafeTextWriter PrepareTextWriter()
        {
            //获得文本编写器
            TextWriter writer = (this.IsWriteToErrorStream) ? Console.Error : Console.Out;

            //返回安全编写器
            return new SafeTextWriter(writer);
        }

        #endregion

        #region ---Private Method

        /// <summary>
        /// 获得不同日志级别信息的渲染颜色
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <returns>日志信息的显示颜色</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/18 11:59    创建
        /// </history>
        private ConsoleColor GetLogLevelReanderColor(Level level)
        {
            //获得日志级别的名称
            if (level == Level.Debug)
            {
                //调试信息
                return ConsoleColor.Gray;
            }
            else if (level == Level.Trace)
            {
                //跟踪信息
                return ConsoleColor.Gray;
            }
            else if (level == Level.Info)
            {
                //提示信息
                return ConsoleColor.White;
            }
            else if (level == Level.Warn)
            {
                //警告信息
                return ConsoleColor.Yellow;
            }
            else if (level == Level.Error)
            {
                //错误信息
                return ConsoleColor.Red;
            }
            else if (level == Level.Fatal)
            {
                //灾难
                return ConsoleColor.Magenta;
            }
            else
            {
                //默认
                return ConsoleColor.White;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 是否将日志信息输出到错误流
        /// </summary>
        private bool m_IsWriteToErrorStream = false;

        #endregion
    }
}
